﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fis.Service.Forms
{
    public partial class PersonEditForm : SaveEditForm
    {
        public PersonEditForm()
        {
            InitializeComponent();
        }

        protected int customKind = 0;
        protected int limit = 1;

        public override void InitializeViewUI()
        {
            if (ViewModel.ContainsKey("audit") && ViewModel["audit"].Equals(1))
            {
                barButtonItem3.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                barButtonItem1.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                barButtonItem2.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                groupMain.Enabled = false;
            }
            else
            {
                barButtonItem3.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                barButtonItem1.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                barButtonItem2.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                groupMain.Enabled = true;
            }

            if (ViewModel.ContainsKey("copy") && ViewModel["copy"].Equals(1))
            {
                ViewProvider.ViewState = ViewState = Agile.Engine.View.ViewState.AddNew;
            }
        }

        public override void InitializeBinding()
        {
            var data = ViewModel["data"] as DataTable;
            data.Rows[0]["FCUSTOMKIND"] = customKind;

            if (ViewState == Agile.Engine.View.ViewState.AddNew)
            {
                data.Rows[0]["FSTATE"] = 0;
                data.Rows[0]["FLIMIT"] = limit;
            }

            ///清除绑定信息
            Fis.Win.Utils.UIUtils.ClearBindings(layoutControl1);
            ///重新绑定，按Tag属性
            Fis.Win.Utils.UIUtils.TagBindings(layoutControl1, data);

            Fis.Win.Utils.UIUtils.LookUpBinding(edState, Fis.Win.Utils.AppSession.StateCache(), "display", "value");

            edPerson.Focus();
        }

        protected override void SaveAndClose()
        {
            Validate();

            if (!this.dxValidationProvider1.Validate())
            {
                return;
            }

            var card = new Agile.Engine.Utils.CardValidUtils(edPassport.Text.Trim());
            if (!card.Validate())
            {
                MessageBox.Show("身份证号码格式不正确，请重新输入");
                return;
            }

            ViewModel["action"] = "save";

            if (ViewProvider.ExecuteUpdate(ViewModel))
            {
                this.Close();
            }
        }

        protected override void SaveAndNew()
        {
            Validate();

            if (!this.dxValidationProvider1.Validate())
            {
                return;
            }

            var card = new Agile.Engine.Utils.CardValidUtils(edPassport.Text.Trim());
            if (!card.Validate())
            {
                MessageBox.Show("身份证号码格式不正确，请重新输入");
                return;
            }

            ViewModel["action"] = "save";

            if (ViewProvider.ExecuteUpdate(ViewModel))
            {
                foreach (var ctl in this.layoutControl1.Controls)
                {
                    var edit = ctl as DevExpress.XtraEditors.BaseEdit;
                    if (edit != null)
                        edit.EditValue = null;
                }

                ViewState = Agile.Engine.View.ViewState.AddNew;
                ViewModel = ViewProvider.InitializeViewModel(new System.Collections.Hashtable());
                InitializeViewUI();
                InitializeBinding();

            }
        }

        protected override void Audit()
        {
            var d = ViewModel["data"] as DataTable;

            if (Agile.Engine.Utils.StringUtils.IsEmpty(d.Rows[0]["SID"]))
            {
                Message = "保存后才能审核";
                return;
            }

            ViewModel["action"] = "audit";

            if (ViewProvider.ExecuteUpdate(ViewModel))
            {
                using (var f = new SaveFileDialog { Filter = " 文本文件 (*.txt) | *.txt " })
                {
                    if (f.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        var data = ViewModel["data"] as DataTable;
                        Agile.Engine.Utils.IOUtils.CreateDirectory(f.FileName);

                        var context = new StringBuilder()
                        .AppendLine(string.Format("帐套编号：{0}", data.Rows[0]["FBOOKID"]))
                        .AppendLine(string.Format("登录名称：{0}", data.Rows[0]["FCONTRACT"]))
                        .AppendLine(string.Format("登录密码：{0}", ViewModel["Ramdom"]));

                        Agile.Engine.Utils.IOUtils.WriteStream(Encoding.UTF8.GetBytes(context.ToString()), f.FileName);
                    }
                }
            }


        }
    }
}
